/**
 * Created with IntelliJ IDEA.
 * User: Manuel Serrano
 * Date: 1/08/13
 * Time: 08:29 AM
 * To change this template use File | Settings | File Templates.
 */

var urlClientes="Cliente_obtenerClientes.action";
var urlAutoComplete="Cliente_obtenerAutocomletar.action";
var accion = "";

$(document).ready(function(){

    $("#cclientes input[name=buscarcliente]").click(function () {
        mostrarEntrada($(this).attr("value"));
    });

    $("#enviarCliente").click(function () {

        validarValor();

    });

    $("#consultacliente").keyup(function () {
        var val=false;
        if(($("#consultacliente").attr("class").indexOf("cadena2")!=-1)&&($("#consultacliente").val().length>3)){
            val = true;
        }
        if(val){

            if(accion=="NC"){
                $("#checktexto").removeClass("oculto");
            }
            $("#enviarCliente").removeClass("oculto");

        }else{
            $("#checktexto").addClass("oculto");
            $("#enviarCliente").addClass("oculto");
        }



    });

    $("#checktexto").click(function () {

        obtenerAutoCompleteJSONData($("#consultacliente").val());

    });



});



function mostrarEntrada(opcion){


    $("#consultacliente").removeClass("oculto");

    if (opcion=="CodServicio"){
        if(accion!="CC"){
            $("#consultacliente").attr("placeholder","Codigo de cliente");
            accion = "CC"
            $("#checktexto").addClass("oculto");
        }

    }else{
        if(accion!="NC"){
            $("#consultacliente").attr("placeholder","Nombre cliente");
            accion = "NC"
            if($("#consultacliente").val().length>3) {
                $("#checktexto").removeClass("oculto");
            }
        }
    }

}

function validarValor(){

    if (!$("#consultacliente").val().isEmpty()){
        obtenerClientesJSONData($("#consultacliente").val(),accion)
    }else{
        notifyNucleo("No debe estar vacio el campo",500,5000);
    }


}



function obtenerClientesJSONData(valor,accion){
    var url = urlClientes;
    var param={};
    param.valor=valor;
    param.accion= accion;

    sendServiceJSON(url,param,obtenerClientesJSONSuccess,null,null);
}


function obtenerClientesJSONSuccess(originalRequest){
    //                   this is the json return data
    var result = originalRequest;

    resultGlobal=result;


    tabler=tablas("consultaclientes","table_clientes",result.tabla,result.clientes,null);


    tabler = tabler.dataTable({
        "bDestroy": true,
        "bJQueryUI": true,
        "bFilter": true,
        "bPaginate" : true,
        "bSort": true,
        "iDisplayLength": 5,
        "aLengthMenu": [5, 10, 15, 20, 25, 100, "all"],
        "aoColumns": [
            { "sClass": "center" },
            { "sClass": "center"},
            {  "sClass": "center" },
            {  "sClass": "center" },
            {  "sClass": "center" },
            {  "sClass": "center" },
            {  "sClass": "center" }
        ],
        /*"sDom": 'T<"clear">lfrtip',
         "sSwfPath": "resources/swf/copy_csv_xls_pdf.swf",
         "oTableTools": {
         "aButtons": [
         "copy",
         "csv",
         "xls",
         {
         "sExtends": "pdf",
         "sPdfOrientation": "landscape",
         "sPdfMessage": "Your custom message would go here."
         },
         "print"
         ]},*/
        "oLanguage": {
            "sZeroRecords": "No Hay Informaci&oacute;n",
            "sInfo": "",
            "sInfoEmpty": ""
        }
    });


    $("#fconsultaclientes").removeClass("oculto");


}



function obtenerAutoCompleteJSONData(valor){
    var url = urlAutoComplete;
    var param={};
    param.valor=valor;

    sendServiceJSON(url,param,obtenerAutoCompleteJSONSuccess,null,null);
}


function obtenerAutoCompleteJSONSuccess(originalRequest){
    //                   this is the json return data
    var result = originalRequest;


    $("#consultacliente").autocomplete({
        source: result.autocompletar,
        autoFill: true,
        mustMatch: true
    }).focus(function () {
            $(this).autocomplete("search", "");
        });

}


